package com.xdf.xzymanagementsystem.util;

/*import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;*/

/**
 * @program: xzy-management-system
 * @description: JWT工具类
 * @author: 张柯
 * @create: 2021-01-19 10:53
 **/
//@Component
public class JWTUtil {
/*    @Value("${jwt.expire-time}")
    private long expireTime;
    private static final String USER_NAME_PREFIX = "USER_NAME_PREFIX";

    *//**
     * 创建JWTToken
     *
     * @param userName
     * @param secret
     * @return
     *//*
    public String createJWTToken(String userName, String secret) {
        Algorithm algorith = Algorithm.HMAC256(secret);
        Date createDate = new Date();
        Date expireDate = new Date(System.currentTimeMillis() + expireTime);
        return JWT.create()
                .withClaim(USER_NAME_PREFIX, userName)
                .withIssuedAt(createDate)
                .withExpiresAt(expireDate)
                .sign(algorith);
    }

    *//**
     * 校验token
     *
     * @param jwtToken
     * @param userName
     * @param secret
     * @return
     *//*
    public boolean verifyJWT(String jwtToken, String userName, String secret) {
        DecodedJWT decodedJWT = getDecodedJWT(jwtToken, userName, secret);
        if (decodedJWT == null) return false;
        return decodedJWT.getExpiresAt().getTime() > System.currentTimeMillis();
    }

    public static DecodedJWT getDecodedJWT(String jwt, String userName, String secret) {
        try {
            Algorithm algorith = Algorithm.HMAC256(secret);
            JWTVerifier jwtVerifier = JWT.require(algorith)
                    .withArrayClaim(USER_NAME_PREFIX, userName)
                    .build();
            DecodedJWT decodedJWT = jwtVerifier.verify(jwt);
            return decodedJWT;
        } catch (Exception e) {
            return null;
        }
    }

    *//**
     * 获取用户
     *
     * @param token
     * @return
     *//*
    public static String getUserNameFromToken(String token) {
        DecodedJWT jwt = JWT.decode(token);
        return jwt.getClaim(USER_NAME_PREFIX).asString();
    }*/
}
